package com.andy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.andy.service.IManageStudentService;
import com.andy.utils.ExcelUtils;
import com.andy.vo.CommonMessageResultVO;
import com.andy.vo.ExamUserVO;

@Controller
@RequestMapping(value = "/teacher")
public class ManageStudentController {
	
	@Autowired
	private IManageStudentService manageStudentService;
	
	/**
	 * 进入学生管理页面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toManageStudents")
	public String toManageStudents(HttpSession session, Model model) {
		List<ExamUserVO> examStudentList = manageStudentService.getAllStudents();	// 进入时读取所有学生信息
		model.addAttribute("examStudentList", examStudentList);
		return "teacher/manageStudents";
	}
	
	@ResponseBody
	@RequestMapping(value = "/uploadFile")
	public CommonMessageResultVO uploadFile(@RequestParam(value = "uploadFile" ) MultipartFile[] uploadFile, HttpSession session, Model model) {
//		final String FILE_SYSTEM_PATH = "E:\\upload\\";
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		MultipartFile uploadSimpleFile = uploadFile[0];
//		File file = new File(FILE_SYSTEM_PATH + uploadSimpleFile.getOriginalFilename());
		String fileName = uploadSimpleFile.getOriginalFilename();
		
		// 获取文件后缀名
		String postfix = fileName.substring(fileName.lastIndexOf("."));
		Map<String, String> messageMap = new HashMap<String, String>();
		
		try {
			// 判断文件类型只能是xlsx, xls
			if (ExcelUtils.OFFICE_EXCEL_2007_POSTFIX.equals(postfix) || ExcelUtils.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
				// uploadSimpleFile.transferTo(file);
				manageStudentService.saveStudentByExcel(uploadSimpleFile.getInputStream(), postfix);
				resultVO.setSuccess(true);
			} else {
				messageMap.put("message", "文件格式不正确。");
				resultVO.setMessageMap(messageMap );
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", "批量导入失败。");
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	/**
	 * 保存学生和科目保存类似
	 * @param examUserVO
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveStudent")
	public CommonMessageResultVO saveStudent(@Valid ExamUserVO examUserVO, BindingResult bindingResult) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			manageStudentService.addStudent(examUserVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	/**
	 * 修改学生，和科目修改类似
	 * @param examUserVO
	 * @param bindingResult
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateStudent")
	public CommonMessageResultVO updateStudent(ExamUserVO examUserVO, BindingResult bindingResult) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		try {
			manageStudentService.updateStudent(examUserVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	/**
	 * 删除学生，和科目删除类似
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteStudent")
	public CommonMessageResultVO deleteStudent(String userId) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		try {
			manageStudentService.deleteStudent(userId);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getStudentById")
	public ExamUserVO getStudentById(String userId) {
		return manageStudentService.getStudentById(userId);
	}
	
}
